package demo.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import demo.entity.Follow;
import demo.entity.User;
import demo.service.FollowService;

@SuppressWarnings("serial")
public class FollowAction extends BaseAction {

	private Follow follow;
	private FollowService followService;
	private String result;
	
	private final static String HQL_FIND_FOLLOW_BY_USERID_AND_FOLLOWID = "from Follow where userid=:userid and followid=:followid";
	
	public String add() throws Exception {
System.out.println("in FollowAction add method...");
		
		User user = (User) getSession().get("user");
		follow.setUserid(user.getId());
		follow.setTime(new Date());
		followService.save(follow);
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "success");
		JSONObject jsonObject = JSONObject.fromObject(map);
		setResult(jsonObject.toString());
		
		return SUCCESS;
	}
	
	public String cancel() throws Exception {
		System.out.println("in FollowAction cancel method...");

		User user = (User) getSession().get("user");
		String[] paramNames = {"userid", "followid"};
		Object[] values = {user.getId(), follow.getFollowid()};
		followService.delete(followService.queryByNameParam(HQL_FIND_FOLLOW_BY_USERID_AND_FOLLOWID, paramNames, values).get(0));
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "success");
		JSONObject jsonObject = JSONObject.fromObject(map);
		setResult(jsonObject.toString());
		
		return SUCCESS;
	}
	
	public String method() throws Exception {
		//	TODO
		return SUCCESS;
	}
	
	
	//	setters and getters
	
	public Follow getFollow() {
		return follow;
	}

	public void setFollow(Follow follow) {
		this.follow = follow;
	}

	public void setFollowService(FollowService followService) {
		this.followService = followService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

}
